home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8008 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: ts4-007.jaxnet.com!user
  2. From: garyg@jax.jaxnet.com (Gary M. Greenberg)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Simple Program Question
  5. Date: Thu, 29 Feb 1996 20:45:25 -0500
  6. Organization: Southeast Network Services, Inc.
  7. Message-ID: <garyg-2902962045250001@ts4-007.jaxnet.com>
  8. References: <4gsr9u$sk6@newsbf02.news.aol.com> <4gti5g$d78@garden.csc.calpoly.edu>
  9. NNTP-Posting-Host: ts4-007.jaxnet.com
  10.  
  11. In article <4gti5g$d78@garden.csc.calpoly.edu>,
  12. dstubbs@garden.csc.calpoly.edu (Dan Stubbs) wrote:
  13.  
  14. >  In article <4gsr9u$sk6@newsbf02.news.aol.com>, Tycope <tycope@aol.com> wrote:
  15. >  >I am trying to write a non -interactive program that calculates all
  16. >  >integer triples (i, j, k) such that 
  17. >  >0 < i < j < k < l and i + j + k = l. Print out the number of triples that
  18. >  >satisfy the requirements and print out every millionth triple.  Can anyone
  19. >  >see where I am missing the boat in the following code.  The program runs
  20. >  >and immediately terminates.  Thanks in advance for any feedback. 
  21. >  >
  22.  
  23. Ty:
  24. All the superflous "()"s in your original code are an indication that you
  25. were trying to 'force' the order of evaluation in the code to make things
  26. work and that maybe this is still rather foreign to ya. No sweat. It is
  27. better imho to liberally sprinkle printf() statements in your code when
  28. you are learning. If you had preceded the do loop with a printf() of the
  29. values of i,j,k,l you would have seen 0 0 0 0 and known that nothing
  30. would occur. The minimal changes below should help you see
  31. what you want.
  32.  
  33. >  >#include <stdio.h>
  34. >  >
  35. >  >long int i, j, k, l;
  36. >  >long int count;
  37. >  >
  38. >  >int
  39. >  >main (void)
  40. >  >{
  41.  
  42.     l=1;
  43.  
  44. >  >       do
  45. >  >       {
  46. >  >               /* (l = i + j + k);*/
  47. >  >               (i = 1);
  48. >  >               (j = 2);
  49. >  >               (k = 3);
  50. >  >               /*(i++, j++, k++);*/
  51.                     l = i + j + k;
  52.                     i++; j++; k++;
  53. >  >               (++count);
  54. >  >       if (count % 1000000 == 0)
  55. >  >        {
  56. >  >               printf("%ld(i) + %ld(j) + %ld(k) = %ld(l)", i, j, k, l);
  57. >  >        }
  58. >  >
  59. >  >       } while (0 < i < j < k < l);
  60. >  >
  61. >  >       return (0);
  62. >  >}
  63. >  
  64. [snip ...]
  65. >  A couple of problems: the first time l = i+j+k is encountered
  66. >  i, j, and k have had no value assigned and hence what happens is undefined.
  67. >  Perhaps that is where your program dies.
  68. Bzzzt! the declared variables have global scope && are each automatically
  69. set to 0 <zero>. That is why the program terminates. The first iteration
  70. is ... "while (0 < i < j < k < 0);"
  71. reason enuf!
  72.  
  73. [big snip ...]
  74.  
  75. C'ya,
  76.  
  77. gary    /* the Sorcerer's Apprentice */
  78.  
  79. Free Speech in America murdered by the Communications Decency Act.
  80.       ((U.S. Congress && Executive) == Collective Moron) == 1
  81.